SET
SMODEL/CGE1,CGE2,CGE3,Enduse1,Enduse2/
SCENARIOcge
SCENARIOend
REMF
VEMF
Y/2005*2050/
SCENARIO/
Baseline
Mitigation
Baseline_low
NDC80_Low
/
;

$gdxin '../data/JPN_emf.gdx'
$load VEMF REMF SCENARIOcge=SCENARIO

$gdxin '../data/AIMEnduse.gdx'
$load SCENARIOend=SCENARIO

*Mapping set
SET
SCENARIOMAP1(SModel,SCENARIOcge,SCENARIO)/
CGE1    .       SSP2_BaU_NoCC   .       Baseline
CGE1    .       SSP2_80NPi_NoCC .       Mitigation
CGE2    .       SSP2_AIMEInv_BaU_NoCC   .       Baseline
CGE2    .       SSP2_AIMEInv_80NPi_NoCC .       Mitigation
CGE3    .       SSP2_AIMEInv2_BaU_NoCC   .       Baseline
CGE3    .       SSP2_AIMEInv2_80NPi_NoCC .       Mitigation
/
SCENARIOMAP2(SMODEL,SCENARIOend,SCENARIO)/
Enduse1  .       Baseline_Fulltech        .       Baseline
Enduse1  .       Mitigation_Fulltech      .       Mitigation
Enduse2  .       Baseline2_Fulltech        .       Baseline
Enduse2  .       Mitigation2_Fulltech      .       Mitigation
/
SGap/
CGE1_2
CGE1_End1
CGE2_End1
CGE3_End2
/
MapGap(Sgap,SMODEL,SMODEL)/
CGE1_2  .       CGE1    .       CGE2
CGE1_End1        .       CGE1    .       Enduse1
CGE2_End1        .       CGE2    .       Enduse1
CGE3_End2        .       CGE3    .       Enduse2
/
Y2020Comp(Y)/
2020,2025,2030,2035,2040,2045,2050/
;
ALIAS(SMODEL,SMODEL1,SMODEL2);

PARAMETER
Enduseload(SCENARIOend,REMF,VEMF,Y)
CGEload(SCENARIOcge,REMF,VEMF,Y)
Dataload(SMODEL,SCENARIO,REMF,VEMF,Y)                  Loaded data merging all
Dataload2020(SMODEL,SCENARIO,REMF,VEMF,Y)
ObsMean(SMODEL,SCENARIO,REMF,VEMF)                       Mean of observation

DegreeOfCoefficient(Sgap,SCENARIO,REMF,VEMF)           Degree of Coefficient
Ratio(Sgap,SCENARIO,REMF,VEMF,Y)
Diff(Sgap,SCENARIO,REMF,VEMF,Y)
MAPE(Sgap,SCENARIO,REMF,VEMF)
MAPE2020(Sgap,SCENARIO,REMF,VEMF)
MAE(Sgap,SCENARIO,REMF,VEMF)
MEAN(Sgap,SCENARIO,REMF,VEMF)
RMSE(Sgap,SCENARIO,REMF,VEMF)
RMSEStand(Sgap,SCENARIO,REMF,VEMF)
MAEStand(Sgap,SCENARIO,REMF,VEMF)

Ratio2005(Sgap,SCENARIO,REMF,VEMF)
Mitbase_ratio(SMODEL,REMF,VEMF,Y)                      Mitigation and Baseline change ratio
RedRate(SMODEL,REMF,VEMF,Y)                            Reduction rate for MAC curve
;

$gdxin '../data/AIMEnduse.gdx'
$load Enduseload=EMFtemp1

$gdxin '../data/JPN_emf.gdx'
$load CGEload=EMFtemp1
SET
VEMFMain(VEMF)/
Fin_Ene
Prc_Car
Prm_Ene
Prm_Ene_Coa
Prm_Ene_Fos
Prm_Ene_Gas
Prm_Ene_Nuc
Prm_Ene_Oil
Sec_Ene_Ele
Sec_Ene_Ele_Bio
Sec_Ene_Ele_Coa
Sec_Ene_Ele_Gas
*Sec_Ene_Ele_NonBioRen
Sec_Ene_Ele_Nuc
Sec_Ene_Ele_Oil
Sec_Ene_Liq
Fin_Ene_Ele
Fin_Ene_Gas
Fin_Ene_Ind
Fin_Ene_Liq
Fin_Ene_Res_and_Com
Fin_Ene_Solids
Fin_Ene_Tra
Prm_Ene_Hyd
Prm_Ene_Solar
Prm_Ene_Win
Sec_Ene
Fin_Ene_Res
Fin_Ene_Com
Emi_CO2_Ene
/
VEMF_Emi(VEMF)/
Emi_CO2_Ene
Emi_CO2_Ene_and_Ind_Pro
Emi_CO2_Ene_Dem
Emi_CO2_Ene_Dem_AFO
Emi_CO2_Ene_Dem_Com
Emi_CO2_Ene_Dem_Ind
Emi_CO2_Ene_Dem_Res
Emi_CO2_Ene_Dem_Res_and_Com
Emi_CO2_Ene_Dem_Tra
Emi_CO2_Ene_Dem_Tra_Fre
Emi_CO2_Ene_Dem_Tra_Pss
Emi_CO2_Ene_Sup
Emi_CO2_Ene_Sup_Ele
Emi_CO2_Ene_Sup_Gas
Emi_CO2_Ene_Sup_Heat
Emi_CO2_Ene_Sup_Liq
Emi_CO2_Ene_Sup_Solids
Fin_Ene_Res_and_Com
Fin_Ene_Ind
Fin_Ene_Tra
Fin_Ene_Res
Fin_Ene_Com
/
;

Dataload(SMODEL,SCENARIO,REMF,VEMF,Y)=SUM(SCENARIOMAP1(SMODEL,SCENARIOcge,SCENARIO),CGEload(SCENARIOcge,REMF,VEMF,Y));
Dataload(SMODEL,SCENARIO,REMF,VEMF,Y)$(SUM(SCENARIOMAP2(SMODEL,SCENARIOend,SCENARIO),Enduseload(SCENARIOend,REMF,VEMF,Y)))=SUM(SCENARIOMAP2(SMODEL,SCENARIOend,SCENARIO),Enduseload(SCENARIOend,REMF,VEMF,Y));

Dataload2020(SMODEL,SCENARIO,REMF,VEMF,Y)$(Dataload(SMODEL,SCENARIO,REMF,VEMF,"2020"))=Dataload(SMODEL,SCENARIO,REMF,VEMF,Y)/Dataload(SMODEL,SCENARIO,REMF,VEMF,"2020");


Ratio(Sgap,SCENARIO,REMF,VEMF,Y)=SUM(MapGap(Sgap,SMODEL1,SMODEL2)$(Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y)),Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)/Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y));
Diff(Sgap,SCENARIO,REMF,VEMF,Y)$(VEMFMain(VEMF))=SUM(MapGap(Sgap,SMODEL1,SMODEL2)$(Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y)),Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)-Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y));
ObsMean(SMODEL,SCENARIO,REMF,VEMF)$(SUM(Y$(Y2020Comp(Y) AND Dataload(SMODEL,SCENARIO,REMF,VEMF,Y)),1))=SUM(Y$(Y2020Comp(Y)),Dataload(SMODEL,SCENARIO,REMF,VEMF,Y))/SUM(Y$(Y2020Comp(Y) AND Dataload(SMODEL,SCENARIO,REMF,VEMF,Y)),1);

DegreeOfCoefficient(Sgap,SCENARIO,REMF,VEMF)$(VEMFMain(VEMF) AND SUM((SMODEL1,SMODEL2)$(MapGap(Sgap,SMODEL1,SMODEL2) AND ObsMean(SMODEL1,SCENARIO,REMF,VEMF)),1))=1-SUM((SMODEL1,SMODEL2)$(MapGap(Sgap,SMODEL1,SMODEL2) AND ObsMean(SMODEL1,SCENARIO,REMF,VEMF)),
    SUM(Y$(Y2020Comp(Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y)),(Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)-Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y))*(Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)-Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y)))/
    SUM(Y$(Y2020Comp(Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),(Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)-ObsMean(SMODEL1,SCENARIO,REMF,VEMF))*(Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)-ObsMean(SMODEL1,SCENARIO,REMF,VEMF)))
   );

MAPE(Sgap,SCENARIO,REMF,VEMF)$(VEMFMain(VEMF) AND SUM((Y,SMODEL1,SMODEL2)$(MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),1))=
  SUM((Y,SMODEL1,SMODEL2)$(MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),ABS((Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)-Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y))/Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y)))/SUM((Y,SMODEL1,SMODEL2)$(MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),1);

MAPE2020(Sgap,SCENARIO,REMF,VEMF)$(VEMFMain(VEMF) AND SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload2020(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload2020(SMODEL1,SCENARIO,REMF,VEMF,Y)),1))=
  SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload2020(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload2020(SMODEL1,SCENARIO,REMF,VEMF,Y)),ABS((Dataload2020(SMODEL1,SCENARIO,REMF,VEMF,Y)-Dataload2020(SMODEL2,SCENARIO,REMF,VEMF,Y))/Dataload2020(SMODEL2,SCENARIO,REMF,VEMF,Y)))/SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload2020(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload2020(SMODEL1,SCENARIO,REMF,VEMF,Y)),1);

MAE(Sgap,SCENARIO,REMF,VEMF)$(VEMFMain(VEMF) AND SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),1))=
  SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),ABS((Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)-Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y))))/
  SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),1);

RMSE(Sgap,SCENARIO,REMF,VEMF)$(VEMFMain(VEMF) AND SUM((Y,SMODEL1,SMODEL2)$(MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),1))=SQRT(
  SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),ABS((Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)-Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y)))**2)/
  SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),1)**2
);

MEAN(Sgap,SCENARIO,REMF,VEMF)$(VEMFMain(VEMF) AND SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),1))=
  SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),((Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)+Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y))))/
  SUM((Y,SMODEL1,SMODEL2)$(Y2020Comp(Y) AND MapGap(Sgap,SMODEL1,SMODEL2) AND Dataload(SMODEL2,SCENARIO,REMF,VEMF,Y) AND Dataload(SMODEL1,SCENARIO,REMF,VEMF,Y)),1)/2
;

RMSEStand(Sgap,SCENARIO,REMF,VEMF)$(MEAN(Sgap,SCENARIO,REMF,VEMF))=RMSE(Sgap,SCENARIO,REMF,VEMF)/MEAN(Sgap,SCENARIO,REMF,VEMF);
MAEStand(Sgap,SCENARIO,REMF,VEMF)$(MEAN(Sgap,SCENARIO,REMF,VEMF))=MAE(Sgap,SCENARIO,REMF,VEMF)/MEAN(Sgap,SCENARIO,REMF,VEMF);

Mitbase_ratio(SMODEL,REMF,VEMF,Y)$(Dataload(SMODEL,"Baseline",REMF,VEMF,Y))=(Dataload(SMODEL,"Mitigation",REMF,VEMF,Y)-Dataload(SMODEL,"Baseline",REMF,VEMF,Y))/Dataload(SMODEL,"Baseline",REMF,VEMF,Y);


*MAC curve information
RedRate(SMODEL,REMF,VEMF,Y)$(Dataload(SMODEL,"Baseline",REMF,VEMF,Y) AND VEMF_Emi(VEMF))=1-Dataload(SMODEL,"Mitigation",REMF,VEMF,Y)/Dataload(SMODEL,"Baseline",REMF,VEMF,Y);
RedRate(SMODEL,REMF,"Prc_Car",Y)=Dataload(SMODEL,"Mitigation",REMF,"Prc_Car",Y);


*Value-added analysis
SET
  A                                                          Activities/
$include ../data/activities.txt
OTH,TOT
  /
A_TOP10(A)/
CSS
OMF
TRS
CNS
CRP
FPR
PPP
I_S
NMM
LIN
OTH
TOT/
;
ALIAS(A,AP);
PARAMETER
  Capitaleff_load(SCENARIOCGE,Y,REMF,A)
  Capitaleff(SMODEL,SCENARIO,Y,REMF,A)
  CapitaleffRel(SMODEL,SCENARIO,Y,REMF,A)
  GDP_psi_load(SCENARIOCGE,Y,REMF,A)
  GDP_psi(SMODEL,SCENARIO,Y,REMF,A)
  VAChangeCapEff(SMODEL,SCENARIO,Y,REMF,A)
  VAChangeCapEff1(SMODEL,SCENARIO,Y,REMF,A)
  GDP_share(SMODEL,SCENARIO,Y,REMF,A)
  GDP_share1(SMODEL,SCENARIO,Y,REMF,A)
;

$gdxin '../data/analysis.gdx'
$load Capitaleff_load=Capitaleff GDP_psi_load=GDP_psi

GDP_psi(SMODEL,SCENARIO,Y,REMF,A)=SUM(SCENARIOMAP1(SMODEL,SCENARIOcge,SCENARIO),GDP_psi_load(SCENARIOCGE,Y,REMF,A));
Capitaleff(SMODEL,SCENARIO,Y,REMF,A)=SUM(SCENARIOMAP1(SMODEL,SCENARIOcge,SCENARIO),Capitaleff_load(SCENARIOCGE,Y,REMF,A));
CapitaleffRel(SMODEL,"Mitigation",Y,REMF,A)$(Capitaleff(SMODEL,"Baseline",Y,REMF,A))=Capitaleff(SMODEL,"Mitigation",Y,REMF,A)/Capitaleff(SMODEL,"Baseline",Y,REMF,A);

VAChangeCapEff(SMODEL,SCENARIO,Y,REMF,A)$(CapitaleffRel(SMODEL,SCENARIO,Y,REMF,A) AND SUM(AP,GDP_psi(SMODEL,SCENARIO,Y,REMF,AP)))=(CapitaleffRel(SMODEL,SCENARIO,Y,REMF,A)-1)*GDP_psi(SMODEL,SCENARIO,Y,REMF,A)/SUM(AP,GDP_psi(SMODEL,SCENARIO,Y,REMF,AP));

GDP_psi(SMODEL,SCENARIO,Y,REMF,"E_BIO")=GDP_psi(SMODEL,SCENARIO,Y,REMF,"E_BIO")+GDP_psi(SMODEL,SCENARIO,Y,REMF,"E_BIN");
GDP_psi(SMODEL,SCENARIO,Y,REMF,"E_BIN")=0;
GDP_share(SMODEL,SCENARIO,Y,REMF,A)$(SUM(AP,GDP_psi(SMODEL,SCENARIO,Y,REMF,AP)))=GDP_psi(SMODEL,SCENARIO,Y,REMF,A)/SUM(AP,GDP_psi(SMODEL,SCENARIO,Y,REMF,AP));

GDP_share1(SMODEL,SCENARIO,Y,REMF,A)$(A_TOP10(A))=GDP_share(SMODEL,SCENARIO,Y,REMF,A);
GDP_share1(SMODEL,SCENARIO,Y,REMF,"TOT")=SUM(A,GDP_share(SMODEL,SCENARIO,Y,REMF,A));
GDP_share1(SMODEL,SCENARIO,Y,REMF,"OTH")=SUM(A,GDP_share(SMODEL,SCENARIO,Y,REMF,A))-SUM(A$(A_TOP10(A)),GDP_share(SMODEL,SCENARIO,Y,REMF,A));

VAChangeCapEff1(SMODEL,SCENARIO,Y,REMF,A)$(A_TOP10(A))=VAChangeCapEff(SMODEL,SCENARIO,Y,REMF,A);
VAChangeCapEff1(SMODEL,SCENARIO,Y,REMF,"TOT")=SUM(A,VAChangeCapEff(SMODEL,SCENARIO,Y,REMF,A));
VAChangeCapEff1(SMODEL,SCENARIO,Y,REMF,"OTH")=SUM(A,VAChangeCapEff(SMODEL,SCENARIO,Y,REMF,A))-SUM(A$(A_TOP10(A)),VAChangeCapEff(SMODEL,SCENARIO,Y,REMF,A));

execute_unload '../output/temp.gdx' Dataload,Ratio,MAPE,MAPE2020,MAE,RMSE,RMSEStand,Mitbase_ratio,MAEStand,DegreeOfCoefficient,ObsMean,RedRate
GDP_psi,VAChangeCapEff,CapitaleffRel,GDP_share
VAChangeCapEff1,GDP_share
;

